
#include "asinh_grad_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>

namespace optiling {
const uint32_t BLOCK_SIZE = 32;
const uint32_t BUFFER_NUM = 2;
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  AsinhGradTilingData tiling;

  uint64_t ubSize;
  auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    // auto socVersion = ascendcPlatform.GetSocVersion();
  ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSize); //获取硬件平台存储空间 UB 的内存大小
    // auto aivNum = ascendcPlatform.GetCoreNum(); //获取当前硬件平台的核数 此平台为1

  std::cout<<"ubSize:"<<ubSize<<std::endl;
    //获取输入shape信息
  uint32_t inputNum = context->GetInputShape(1)->GetStorageShape().GetShapeSize();
  uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType());
  uint32_t inputLength = inputBytes * inputNum;

    //可使用的ub空间 输入3输出1
  uint32_t ubDataNumber = (inputBytes == 2) ? 12 : 6;

  uint32_t tileBlockNum = (ubSize / BLOCK_SIZE ) /ubDataNumber;

  uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / inputBytes;

  std::cout<<"tileDataNum:"<<tileDataNum<<std::endl;
    // Input data for 32B alignment
  uint32_t inputLengthAlgin32 = (((inputLength + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE);
    // There is at least 32B of data on each core, satisfying several settings for several cores. The maximum number of audits is the actual number of audits
  uint32_t everyCoreInputBlockNum = inputLengthAlgin32 / BLOCK_SIZE;// aivNum;
    
    //  chunks are calculated and sliced several times using the number of data on each core
  uint32_t CoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / inputBytes;

  std::cout <<"CoreDataNum:"<<CoreDataNum<<std::endl;
  uint32_t TileNum = everyCoreInputBlockNum / tileBlockNum;
  uint32_t finalTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? TileNum : TileNum + 1;
    // Tail block calculation for  chunks of data

  std::cout << "finalTileNum:"<<finalTileNum<<std::endl;
  uint32_t TailDataNum = CoreDataNum - (tileDataNum * TileNum); 
  TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum;
  std::cout <<"TailDataNum:"<<TailDataNum<<std::endl;


  //if 

  tiling.set_CoreDataNum(CoreDataNum);
  tiling.set_tileDataNum(tileDataNum);
  tiling.set_TailDataNum(TailDataNum);
  tiling.set_finalTileNum(finalTileNum);





  context->SetBlockDim(1);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
  size_t *currentWorkspace = context->GetWorkspaceSizes(1);
  currentWorkspace[0] = 0;

  return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class AsinhGrad : public OpDef {
public:
    explicit AsinhGrad(const char* name) : OpDef(name)
    {
        this->Input("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("dy")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("z")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(AsinhGrad);
}
